package cn.zhku.news.web.servlet;

import cn.itcast.servlet.BaseServlet;
import cn.zhku.news.domain.News;
import cn.zhku.news.service.NewsService;
import net.sf.json.JSONSerializer;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/NewsServlet")
public class NewsServlet extends BaseServlet {
       
	private static final long serialVersionUID = 1L;
	NewsService newsService = new NewsService();

	public News editNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		return null;
	}
	
	public String showAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8");
		ArrayList<News> newsList = new ArrayList<News>();
		newsList = (ArrayList<News>) newsService.showAll();
		request.setAttribute("newsList", newsList);
		return "list_new.jsp";
	}
	
	public void newsJson(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8");
		ArrayList<News> newsList = new ArrayList<News>();
		newsList = (ArrayList<News>) newsService.showAll();
		 String newsJson = JSONSerializer.toJSON(newsList).toString();
		
		 response.getWriter().print(newsJson);
		 
	}
	


}
